<template>
	<view class="detail">
		<c-section :title="deviceName" action="log" @click="handleLog"></c-section>
		<view class="wrapper">
			<view v-if="modelInfo.modelTxt==='LPC100'">
				<button @click="handleWrite(1)">写入LPC100</button>
			</view>
			<view v-if="modelInfo.modelTxt==='LPC110'">
				<button @click="handleWrite(1)">写入LPC110</button>
			</view>
			<view v-if="modelInfo.modelTxt==='WAC710'">
				<button @click="handleWrite(2)">写入WAC710</button>
			</view>
			<view>
				<button @click="handleDisconnect">断开连接</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	// #ifdef APP-PLUS
	import mixBluetooth from '@/mixins/bluetooth-plus.js';
	// #endif
	// #ifndef APP-PLUS
	import mixBluetooth from '@/mixins/bluetooth.js';
	// #endif
	import mixDevice from '@/mixins/device.js';

	export default {
		data() {
			return {
				deviceId: '',
				deviceName: ''
			};
		},
		computed: {
			...mapGetters('device', ['modelInfo'])
		},
		mixins: [mixBluetooth, mixDevice],
		onLoad() {
			this.deviceId = uni.getStorageSync('deviceId');
			this.deviceName = uni.getStorageSync('deviceName');

			const serviceId = uni.getStorageSync('serviceId');
			if (!serviceId) {
				if (this.$store.state.system.platform === 'android') {
					// android需要设置MTU
					this.setMTU(this.deviceId, () => {
						// 获取服务以及特征值
						this.getDeviceServices(this.deviceId);
					});
				} else {
					// 获取服务以及特征值
					this.getDeviceServices(this.deviceId);
				}
			}
		},
		methods: {
			handleWrite(type) {
				let buffer, dataView, index = 0;

				switch (type) {
					case 1:
						buffer = new ArrayBuffer(19);
						dataView = new DataView(buffer);
						dataView.setUint8(index++, 0x68);
						dataView.setUint8(index++, 0x0d);
						dataView.setUint8(index++, 0x00);
						dataView.setUint8(index++, 0x68);
						dataView.setUint8(index++, 0x40);
						dataView.setUint8(index++, 0xfe);
						dataView.setUint8(index++, 0x09);
						dataView.setUint8(index++, 0x59);
						dataView.setUint8(index++, 0x22);
						dataView.setUint8(index++, 0x04);
						dataView.setUint8(index++, 0x00);
						dataView.setUint8(index++, 0x16);
						dataView.setUint8(index++, 0x29);
						dataView.setUint8(index++, 0x0a);
						dataView.setUint8(index++, 0xff);
						dataView.setUint8(index++, 0xfe);
						dataView.setUint8(index++, 0x00);
						dataView.setUint8(index++, 0x0c);
						dataView.setUint8(index++, 0x16);
						break;
					case 2:
						buffer = new ArrayBuffer(16);
						dataView = new DataView(buffer);
						dataView.setUint8(index++, 0x68);
						dataView.setUint8(index++, 0x0a);
						dataView.setUint8(index++, 0x00);
						dataView.setUint8(index++, 0x68);
						dataView.setUint8(index++, 0x40);
						dataView.setUint8(index++, 0x01);
						dataView.setUint8(index++, 0x12);
						dataView.setUint8(index++, 0x34);
						dataView.setUint8(index++, 0x56);
						dataView.setUint8(index++, 0x78);
						dataView.setUint8(index++, 0x05);
						dataView.setUint8(index++, 0xff);
						dataView.setUint8(index++, 0x0b);
						dataView.setUint8(index++, 0x01);
						dataView.setUint8(index++, 0x65);
						dataView.setUint8(index++, 0x16);
						break;
				}

				this.writeCharacteristicValue(this.deviceId, buffer);
			},
			handleLog() {
				uni.navigateTo({
					url: '../log/log'
				});
			},
			handleDisconnect() {
				this.disconnectDevice(this.deviceId, this.deviceName, () => {
					uni.navigateBack();
				});
			},
			...mapMutations('log', ['addOprLog'])
		}
	}
</script>

<style lang="scss">
	.detail {
		.wrapper {
			padding: 0 20rpx;
		}
	}
</style>
